package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.minordeath.riskstrategy;

import com.bmps.springcloud.insuredmicroservice.constant.accept.StrategyConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.LcPolModel;
import com.bmps.springcloud.insuredmicroservice.repository.calculate.coresystem.AccountValueRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * description:
 * LBT030
 *
 * @author jackdaw
 * @date 2020-01-07 20:33
 */
@Component
public class Lbt030RiskStrategy implements RiskStrategy {

    private final RedisService redisService;
    private final AccountValueRepository accountValueRepository;

    public Lbt030RiskStrategy(RedisService redisService, AccountValueRepository accountValueRepository) {
        this.redisService = redisService;
        this.accountValueRepository = accountValueRepository;
    }

    @Override
    public BigDecimal getAmount(LcPolModel lcPolModel, List<LcPolModel> lcPolModels, boolean current) {
        //获取主险信息
        LcPolModel mainPol = null;
        if (current) {
            for (LcPolModel model : lcPolModels) {
                if (redisService.isMainRisk(model.getRiskCode())) {
                    mainPol = model;
                }
            }
        } else {
            mainPol = accountValueRepository.queryMainPol(lcPolModel.getPrtNo());
        }

        if (StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge() >= lcPolModel.getPayYears()) {
            //如果（StrategyConstant.ADULT_AGE-被保险人的投保年龄）大于等于缴费期，则未成年人的身故保额设置为（主险保费*缴费期，险种保费的60%*缴费期）比较取较大值
            if (mainPol.getPrem().multiply(BigDecimal.valueOf(lcPolModel.getPayYears())).compareTo(lcPolModel.getPrem().multiply(BigDecimal.valueOf(0.6 * lcPolModel.getPayYears()))) == 1) {
                return mainPol.getPrem().multiply(BigDecimal.valueOf(lcPolModel.getPayYears()));
            } else {
                return lcPolModel.getPrem().multiply(BigDecimal.valueOf(0.6 * lcPolModel.getPayYears()));
            }
        } else if (StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge() < lcPolModel.getPayYears() && StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge() > 0) {
            //如果（StrategyConstant.ADULT_AGE-被保险人的投保年龄）小于缴费期并且大于0，则未成年人的身故保额设置为（主险保费*（StrategyConstant.ADULT_AGE-被保险人的投保年龄），险种保费的60%*（StrategyConstant.ADULT_AGE-被保险人的投保年龄））比较取较大值；
            if (mainPol.getPrem().multiply(BigDecimal.valueOf(StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge())).compareTo(lcPolModel.getPrem().multiply(BigDecimal.valueOf(0.6 * (StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge())))) == 1) {
               return  mainPol.getPrem().multiply(BigDecimal.valueOf(StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge()));
            } else {
                return lcPolModel.getPrem().multiply(BigDecimal.valueOf(0.6 * (StrategyConstant.ADULT_AGE - lcPolModel.getInsuredAppAge())));
            }
        }
        return BigDecimal.valueOf(0);
    }
}
